package com.zry.yard.presenter

import android.content.Intent
import android.view.View
import com.zry.yard.R
import com.zry.yard.adapter.DistributorListAdapter
import com.zry.yard.animation.RecyclerViewUtilKt
import com.zry.yard.been.CustomerInfo
import com.zry.yard.retrofit.HttpServer
import com.zry.yard.retrofit.ShowUserView
import com.zry.yard.ui.activity.DistributorInfoActivity
import com.zry.yard.ui.activity.DistributorListActivity
import com.zry.yard.utils.ACacheUtil
import kotlinx.android.synthetic.main.activity_distributor_list.*

class DistributorListPresenter(private val activity: DistributorListActivity) : ShowUserView {

    private var distributorListAdapter: DistributorListAdapter? = null
    private var customerInfoList: MutableList<CustomerInfo>? = null
    fun distributorRecord(view: View) {
        when (view.id) {
            R.id.searchDistributorBtn -> {
                customerInfoList!!.removeAll(customerInfoList!!)
                distributorListAdapter?.notifyDataSetChanged()
                getCustomerList(activity.searchDistributorET.text.toString().trim(), 1, 999)
            }
        }
    }

    fun initRecycler() {
        customerInfoList = mutableListOf()
        val recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.distributorRecycler)
        recyclerViewUtilKt.initRecyclerView()
        distributorListAdapter = DistributorListAdapter(R.layout.item_distributor_list, customerInfoList)
        recyclerViewUtilKt.setAdapter(distributorListAdapter!!)
        distributorListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, DistributorInfoActivity::class.java)
            intent.putExtra("CustomerInfo", customerInfoList!![position])
            activity.startActivity(intent)
        }
    }

    fun getCustomerList(CustomerName: String, pageIndex: Int, pageSize: Int) {
        HttpServer(this).getCustomerList(ACacheUtil().getLoginInfo().EntID, CustomerName, pageIndex, pageSize, activity)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            34 -> {
                val customerInfoBeanList = data as MutableList<CustomerInfo>
                customerInfoList!!.removeAll(customerInfoList!!)
                customerInfoList!!.addAll(customerInfoBeanList)
                distributorListAdapter?.notifyDataSetChanged()
            }
        }
    }
}